package com.ruoyi.compensationManagement.service.impl;

import java.util.List;

import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.system.mapper.SysUserPositionLevelMapper;
import com.ruoyi.system.mapper.SysUserPositionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.compensationManagement.mapper.PositionSettingsMapper;
import com.ruoyi.compensationManagement.domain.PositionSettings;
import com.ruoyi.compensationManagement.service.IPositionSettingsService;

/**
 * 岗位设置Service业务层处理
 * 
 * @author yuan
 * @date 2022-10-05
 */
@Service
public class PositionSettingsServiceImpl implements IPositionSettingsService 
{
    @Autowired
    private PositionSettingsMapper positionSettingsMapper;
    @Autowired
    private SysUserPositionMapper sysUserPositionMapper;
    @Autowired
    private SysUserPositionLevelMapper sysUserPositionLevelMapper;

    /**
     * 查询岗位设置
     * 
     * @param positionId 岗位设置主键
     * @return 岗位设置
     */
    @Override
    public PositionSettings selectPositionSettingsByPositionId(Long positionId)
    {
        return positionSettingsMapper.selectPositionSettingsByPositionId(positionId);
    }

    /**
     * 查询岗位设置列表
     * 
     * @param positionSettings 岗位设置
     * @return 岗位设置
     */
    @Override
    public List<PositionSettings> selectPositionSettingsList(PositionSettings positionSettings)
    {
        return positionSettingsMapper.selectPositionSettingsList(positionSettings);
    }

    /**
     * 新增岗位设置
     * 
     * @param positionSettings 岗位设置
     * @return 结果
     */
    @Override
    public int insertPositionSettings(PositionSettings positionSettings)
    {
        return positionSettingsMapper.insertPositionSettings(positionSettings);
    }

    /**
     * 修改岗位设置
     * 
     * @param positionSettings 岗位设置
     * @return 结果
     */
    @Override
    public int updatePositionSettings(PositionSettings positionSettings)
    {
        return positionSettingsMapper.updatePositionSettings(positionSettings);
    }

    /**
     * 批量删除岗位设置
     * 
     * @param positionIds 需要删除的岗位设置主键
     * @return 结果
     */
    @Override
    public int deletePositionSettingsByPositionIds(Long[] positionIds)
    {
        for (Long positionId : positionIds) {
            //判断删除项是否有子类
            List<PositionSettings> positionSettingsList = positionSettingsMapper.selectChildNodeByPositionId(positionId);
            boolean flag = true;
            if(positionSettingsList != null && positionSettingsList.size() > 0){
                flag = false;
                for (PositionSettings positionSettings : positionSettingsList) {
                    //判断岗位类型当中的岗位级别是否被使用
                    int i = sysUserPositionLevelMapper.selectByPostId(positionSettings.getPositionId());
                    if(i > 0){
                        throw new GlobalException("岗位等级已被使用，无法删除岗位类型");
                    }
                }
            }
            //没有子节点说明是本身为叶子节点===>判断岗位级别是否被使用
            if(flag){
                int i = sysUserPositionLevelMapper.selectByPostId(positionId);
                if(i > 0){
                    throw new GlobalException("岗位级别已被使用，无法删除");
                }
            }
        }
        return positionSettingsMapper.deletePositionSettingsByPositionIds(positionIds);
    }

    /**
     * 删除岗位设置信息
     * 
     * @param positionId 岗位设置主键
     * @return 结果
     */
    @Override
    public int deletePositionSettingsByPositionId(Long positionId)
    {
        //判断删除项是否有子类
        List<PositionSettings> positionSettingsList = positionSettingsMapper.selectChildNodeByPositionId(positionId);
        boolean flag = false;
        if(positionSettingsList != null && positionSettingsList.size() > 0){
            flag = true;
            for (PositionSettings positionSettings : positionSettingsList) {
                //判断岗位类型当中的岗位级别是否被使用
                int i = sysUserPositionLevelMapper.selectByPostId(positionSettings.getPositionId());
                if(i > 0){
                    throw new GlobalException("岗位等级已被使用，无法删除岗位类型");
                }
            }
        }
        //判断岗位级别是否被使用
        if(flag){
            int i = sysUserPositionLevelMapper.selectByPostId(positionId);
            if(i > 0){
                throw new GlobalException("岗位级别已被使用，无法删除");
            }
        }
        return positionSettingsMapper.deletePositionSettingsByPositionId(positionId);
    }

    /**
     * 通过用户id查找对应的岗位薪资
     * @param userId
     * @return
     */
    @Override
    public PositionSettings selectPositionSettingsByUserId(Long userId){
        return positionSettingsMapper.selectPositionSettingsByUserId(userId);
    }
}
